package com.swu.pad.assignment.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.swu.pad.assignment.dao.AssignmentPadDao;
import com.swu.pad.assignment.entity.Assignment;
import com.swu.pad.assignment.entity.AssignmentPad;
import com.swu.pad.assignment.service.AssignmentPadService;
import com.swu.pad.comment.dao.CommentQuestionDao;
import com.swu.pad.comment.entity.CommentQuestion;
import com.swu.pad.course.entity.Group;
import com.swu.pad.reflection.dao.RftDimTipsDao;
import com.swu.pad.reflection.dao.RftDimTipsDao;
import com.swu.pad.reflection.entity.RftDimTips;
import com.swu.pad.util.StringUtil;
import com.swu.pad.util.EPLiteClient.EPLiteClient;
import com.swu.pad.util.EPLiteClient.EPLiteException;

@Service
public class AssignmentPadServiceImpl implements AssignmentPadService {

	@Autowired
	private AssignmentPadDao assignmentPadDao;
	@Autowired
	private EPLiteClient epliteClient;
	@Autowired
	private CommentQuestionDao commentQuestionDao;
	@Autowired
	private RftDimTipsDao rftDimTipsDao;

	

	private Map<String, Object> getTemplateQuestions(Assignment assignment) {
		List<CommentQuestion> questionList = commentQuestionDao.findByTemplateId(assignment.getReplyTemplateId());
		Map<String, Object> templateMap = new HashMap<String, Object>();
		List<Map<String, String>> multipleList = new ArrayList<Map<String, String>>();
		List<Map<String, String>> singleList = new ArrayList<Map<String, String>>();

		if (questionList != null && questionList.size() > 0) {
			int max = 0;
			for (int i = 0; i < questionList.size(); i++) {
				if (Integer.parseInt(questionList.get(i).getSid()) >= max) {
					max = Integer.parseInt(questionList.get(i).getSid());
				}
			} // 知道了最大的下标
			for (int i = 0; i < max; i++) {
				Map<String, String> questionMap = new HashMap<String, String>();
				int count = 1;
				String score = "";
				for (int j = 0; j < questionList.size(); j++) {
					if (questionList.get(j).getSid().equals(Integer.toString(i + 1))) {
						questionMap.put(questionList.get(j).getId(), questionList.get(j).getQuestion());
						score = questionList.get(j).getScore();
						count++;
					}
				}
				if (!score.equals("")) {// 说明sid不是空
					questionMap.put("score", score);
					if (count > 2) {
						multipleList.add(questionMap);
					} else {
						singleList.add(questionMap);
					}
				}
			}
		}
		templateMap.put("multipleList", multipleList);
		templateMap.put("singleList", singleList);
		return templateMap;
	}

	@Override
	public JSONObject getVersion(String groupName,String assignmentId) {
		JSONObject map=new JSONObject();
//		List mapAll=new ArrayList();
		AssignmentPad asspad=assignmentPadDao.findAssignmentPadByGroupNameAndAssignmentId(groupName,assignmentId);
		if(asspad != null){
//		 Map count=epliteClient.getRevisionsCount(asspad.getId());		
//		 int countNum=Integer.parseInt(count.get("revisions").toString())/1000+1;		 
//		 while(mapAll.size()<Integer.parseInt(count.get("revisions").toString())-1){			 
			  map = epliteClient.getAllRevisionsJson(asspad.getId());	
//			 List list=(List) map.get("version");
//			 mapAll.addAll(list);
//		}
		}
//		Set versionSet=new HashSet();
//		versionSet.addAll(mapAll);
//		result.put("version", versionSet);
		return map;
	}

	@Override
	public AssignmentPad addAssignmentPad(Assignment assignment, Group group) {
		// 获取评论模板
		Map<String, Object> templateMap = getTemplateQuestions(assignment);

		AssignmentPad assignmentPad = new AssignmentPad();
		assignmentPad.setAssignmentId(assignment.getId());
		assignmentPad.setAssignmentName(assignment.getAssignmentName());
		assignmentPad.setCreateTime(new Date());
		assignmentPad.setGroupId(group.getId());
		assignmentPad.setGroupName(group.getGroupName());
		// z s

		// z e
		try {
			Map<?, ?> map = epliteClient.createGroupPad(assignmentPad.getGroupId(), assignmentPad.getAssignmentName(), assignment.getAssignmentContent());
			epliteClient.setPublicStatus(group.getId() + "$" + assignmentPad.getAssignmentName(), true);
			assignmentPad.setId(map.get("padID").toString());
			assignmentPadDao.saveAndFlush(assignmentPad);
		} catch (EPLiteException e) {
			assignmentPad.setId(group.getId() + "$" + assignmentPad.getAssignmentName());
			assignmentPadDao.saveAndFlush(assignmentPad);
		}

		// zdb s
		if (!templateMap.isEmpty()) {
			templateMap.put("assignmentId", assignment.getId());
			assignmentPad = assignmentPadDao.findAssignmentPadByAssignmentIdAndGroupId(assignment.getId(), group.getId());
			epliteClient.createCommentTemplate(assignmentPad.getId(), templateMap);
		}
		return assignmentPad;
	}

}
